//
// less.render('.class { width: (1 + 1) }',
//     {
//         paths: ['.', './lib'],  // Specify search paths for @import directives
//         filename: 'style.less', // Specify a filename, for better error messages
//         compress: true          // Minify CSS output
//     },
//     function (e, output) {
//         console.log(output.css);
//     });


const chokidar = require('chokidar');
var exec = require('child_process').exec
const fs = require('fs')
var babelNode = "./node_modules/.bin/babel"
var defConf = {
    "encode": "utf-8",
    "ignore": 'combo',
    "input": "css",
    "lessInput": "other/less"
}

function getFileNameFromPath(path) {
    var _array = path.split('/')
    var fileName = _array[_array.length - 1]
    fileName = fileName.split('.')[0]
    return fileName
}

function getPathWithouNameFromPath(path) {
    var _array = path.split('/')
    _array = _array.splice(0, _array.length - 1)
    return _array.join("/")
}

function ts2js(path) {
    var fileName = getFileNameFromPath(path)
    var _path = getPathWithouNameFromPath(path)
    exec(`${babelNode}  ${path}  `, {encoding: defConf.encode},

        function (err, stdout, stderr) {
            if (err != null) {
                console.error(err)
                return false
                // throw new Error(err);
            }
            fs.writeFileSync(`${_path}/${fileName}.js`, stdout)
        }
    );
}

ts2js("./src/index.ts")


// One-liner for current directory
chokidar.watch(['./src/**/*.ts'])
    .on('change', (path) => {
        console.log("change", path);
        ts2js("./" + path)
    });
console.log("less 文件开始监听 【styles components  pages】的文件")
